package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsSite;
import com.jeegit.ecms.service.ICmsSiteService;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.core.page.TableDataInfo;

/**
 * 站点设置Controller
 * 
 * @author jeegit
 * @date 2022-06-24
 */
@Controller
@RequestMapping("/ecms/site")
public class CmsSiteController extends BaseController
{
    private String prefix = "ecms/site";

    @Autowired
    private ICmsSiteService cmsSiteService;

    @RequiresPermissions("ecms:site:view")
    @GetMapping()
    public String site()
    {
        return prefix + "/site";
    }

    /**
     * 查询站点设置列表
     */
    @RequiresPermissions("ecms:site:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsSite cmsSite)
    {
        startPage();
        List<CmsSite> list = cmsSiteService.selectCmsSiteList(cmsSite);
        return getDataTable(list);
    }

    /**
     * 导出站点设置列表
     */
    @RequiresPermissions("ecms:site:export")
    @Log(title = "站点设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsSite cmsSite)
    {
        List<CmsSite> list = cmsSiteService.selectCmsSiteList(cmsSite);
        ExcelUtil<CmsSite> util = new ExcelUtil<CmsSite>(CmsSite.class);
        return util.exportExcel(list, "站点设置数据");
    }

    /**
     * 新增站点设置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存站点设置
     */
    @RequiresPermissions("ecms:site:add")
    @Log(title = "站点设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsSite cmsSite)
    {
        return toAjax(cmsSiteService.insertCmsSite(cmsSite));
    }

    /**
     * 修改站点设置
     */
    @RequiresPermissions("ecms:site:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsSite cmsSite = cmsSiteService.selectCmsSiteById(id);
        try {
			System.out.println(JsonMapper.builder().build().writeValueAsString(cmsSite));
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        mmap.put("cmsSite", cmsSite);
        return prefix + "/edit";
    }

    /**
     * 修改保存站点设置
     */
    @RequiresPermissions("ecms:site:edit")
    @Log(title = "站点设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsSite cmsSite)
    {
        return toAjax(cmsSiteService.updateCmsSite(cmsSite));
    }

    /**
     * 删除站点设置
     */
    @RequiresPermissions("ecms:site:remove")
    @Log(title = "站点设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsSiteService.deleteCmsSiteByIds(ids));
    }
}
